/**
 * flat() 方法创建一个新的数组，并根据指定深度递归地将所有子数组元素拼接到新的数组中。
 * flat(depth) depth默认为1
 */

const arr1 = [0, 1, 2, [3, 4]];

console.log(arr1.flat());
// expected output: Array [0, 1, 2, 3, 4]

const arr2 = [0, 1, [2, [3, [4, 5]]]];

console.log(arr2.flat());
// expected output: Array [0, 1, 2, Array [3, Array [4, 5]]]

console.log(arr2.flat(2));
// expected output: Array [0, 1, 2, 3, Array [4, 5]]

console.log(arr2.flat(Infinity));
// expected output: Array [0, 1, 2, 3, 4, 5]


/**
 * flatMap(fn)
 * 等价于flat().map(fn)
 */
const arr3 = [1, 2, 1];
const result = arr3.flatMap((num) => (num === 2 ? [2, 2] : 1));

console.log(arr3) // [1, 2, 1]
console.log(result); // [1, 2, 2, 1]